//
//  AP_String_Extension.swift
//  APSwift
//
//  Created by AbnerPei on 2021/12/1.
//

import Foundation

extension String {
//    func firstRange(of serachingString: String) -> NSRange? {
//        if let range = range(of: serachingString, options: .literal, locale: nil), !range.isEmpty {
//            di
//        }
//    }
}


extension String {
    func ranges(of string: String) -> [Range<String.Index>] {
        var rangeArray = [Range<String.Index>]()
        var searchedRange: Range<String.Index>
        guard let sr = self.range(of: self) else {
            return rangeArray
        }
        searchedRange = sr
        
        var resultRange = self.range(of: string, options: .regularExpression, range: searchedRange, locale: nil)
        while let range = resultRange {
            rangeArray.append(range)
            searchedRange = Range(uncheckedBounds: (range.upperBound, searchedRange.upperBound))
            resultRange = self.range(of: string, options: .regularExpression, range: searchedRange, locale: nil)
        }
        return rangeArray
    }
    
    func nsranges(of string: String) -> [NSRange] {
        var rangeArray = [NSRange]()
        var searchedRange: Range<String.Index>
        guard let sr = self.range(of: self) else {
            return rangeArray
        }
        searchedRange = sr
        
        var resultRange = self.range(of: string, options: .regularExpression, range: searchedRange, locale: nil)
        while let range = resultRange {
            let nsrange = NSRange(range, in: self)
            rangeArray.append(nsrange)
            searchedRange = Range(uncheckedBounds: (range.upperBound, searchedRange.upperBound))
            resultRange = self.range(of: string, options: .regularExpression, range: searchedRange, locale: nil)
        }
        return rangeArray
    }
}

extension String {    
    /// 从指定位置开始截取
    ///
    ///     -------
    ///     示例:
    ///     -------
    ///     let s = "Hello World"
    ///     /// 正常示例
    ///     let from1 = s.subString(from: 1)
    ///     debugPrint("from1 is \(from1)")
    ///
    ///     /// 异常示例
    ///     let from2 = s.subString(from: -1)
    ///     let from3 = s.subString(from: s.count + 2)
    ///     debugPrint("from2 is \(from2), from3 is \(from3)")
    ///
    ///     -------
    ///     控制台log:
    ///     -------
    ///     "from1 is ello World"
    ///     "from2 is Hello World, from3 is Hello World"
    ///
    /// - Parameter from: 指定位置
    /// - Returns: 截取的字符串
    func subString(from: Int) -> String {
        if from < 0 || from > count {
            /// Fatal error: String index is out of bounds
            return self
        } else {
            let idx = index(startIndex, offsetBy: from)
            return String(self[idx ..< endIndex])
        }
    }

    /// 截取到指定位置
    /// - Parameter to: 指定位置
    /// - Returns: 截取的字符串
    func subString(to: Int) -> String {
        if to < 0 || to > count {
            /// Fatal error: String index is out of bounds
            return self
        } else {
            let idx = index(startIndex, offsetBy: to)
            return String(self[..<idx])
        }
    }
    
}
